package edu.scau.mis.web.controller.pos;

import edu.scau.mis.core.annotation.RequestLog;
import edu.scau.mis.core.enums.BusinessType;
import edu.scau.mis.pos.domain.Order;
import edu.scau.mis.pos.service.IOrderService;
import edu.scau.mis.core.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
 * (Order)表控制层
 *
 * @author CJW
 * @since 2024-04-04 00:21:48
 */
@RestController
@RequestMapping("order")
public class OrderController {
    /**
     * 服务对象
     */
    @Autowired
    private IOrderService orderService;

    /**
     * 查询数据列表
     *
     * @param order 筛选条件
     * @return 查询结果
     */
    @RequestLog(title = "查询数据列表",businessType = BusinessType.GET)
    @GetMapping("/list")
    @ApiOperation("查询数据列表")
    public AjaxResult list(Order order) {
        return AjaxResult.success(this.orderService.selectList(order));
    }

    /**
     * 分页查询数据列表
     *
     * @param order 筛选条件
     * @return 查询结果
     */
    @RequestLog(title = "分页查询数据列表",businessType = BusinessType.GET)
    @GetMapping("/page")
    @ApiOperation("分页查询数据列表")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Order order) {
        // Step 1: 执行总数统计查询
        long total = this.orderService.count(order);

        // Step 2: 计算分页的起始位置和结束位置
        int start = (pageNum - 1) * pageSize;
        int end = pageNum * pageSize;

        // Step 3: 执行实际的分页查询
        List<Order> list = this.orderService.selectList(order, start, end);

        return AjaxResult.success(list);
    }

    /**
     * 通过主键查询单条数据以完善支付信息
     *
     * @param orderId 主键
     * @return 单条数据
     */
    @RequestLog(title = "通过主键查询单条数据以完善支付信息",businessType = BusinessType.GET)
    @GetMapping("/pay/{orderId}")
    @ApiOperation("通过主键查询单条数据以完善支付信息")
    public AjaxResult getPayById(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(this.orderService.selectPayById(orderId));
    }
    /**
     * 通过主键查询单条数据
     *
     * @param orderId 主键
     * @return 单条数据
     */
    @RequestLog(title = "查询单条数据",businessType = BusinessType.GET)
    @GetMapping("{orderId}")
    @ApiOperation("通过主键查询单条数据")
    public AjaxResult getById(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(this.orderService.selectById(orderId));
    }

    /**
     * 新增数据
     *
     * @param order 实体
     * @return 新增结果
     */
    @RequestLog(title = "新增订单数据",businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增数据")
    public AjaxResult add(@RequestBody Order order) {
        // 调用Service层进行数据库操作
        orderService.createOrder(order);
        return AjaxResult.success("新增成功！");
    }

    /**
     * 编辑数据
     *
     * @param order 实体
     * @return 编辑结果
     */
    @RequestLog(title = "修改订单数据",businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("编辑数据")
    public AjaxResult edit(@RequestBody Order order) {
        return AjaxResult.success(this.orderService.update(order));
    }

    /**
     * 批量删除数据
     *
     * @param orderId 主键
     * @return 删除是否成功
     */
    @RequestLog(title = "删除订单数据",businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderId}")
    @ApiOperation("批量删除数据")
    public AjaxResult deleteById(@PathVariable Long[] orderId) {
        this.orderService.deleteOrderItemsAndOrders(orderId);
        return AjaxResult.success("批量删除成功！");
    }

    @RequestLog(title = "挂账",businessType = BusinessType.OTHER)
    @DeleteMapping("/start/{orderId}")
    @ApiOperation("修改数据")
    public AjaxResult startById(@PathVariable Long[] orderId) {
        this.orderService.startOrderItemsAndOrders(orderId);
        return AjaxResult.success("挂账成功！");

    }

    @RequestLog(title = "使用次卡",businessType = BusinessType.OTHER)
    @DeleteMapping("/item/{orderId}")
    @ApiOperation("使用次数")
    public AjaxResult useVip(@PathVariable Long orderId){
        return AjaxResult.success(this.orderService.useVip(orderId));
    }
}
